package tid.proyect.controllers;

import tid.proyect.views.EditImageView;
import jjil.android.RgbImageAndroid;
import jjil.core.RgbImage;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class EditImage{
	
	private static RgbImage eRgbImage;
	private RgbImage nRgbImage;
	private Bitmap bitmap;
	private static int height;
	private static int width;
	private EditImageView eView;

	
	
	public EditImage() {
		
		
	}
	
	public Bitmap test(Bitmap bmp){
		eRgbImage = RgbImageAndroid.toRgbImage(bmp);
		height = eRgbImage.getHeight();
		width = eRgbImage.getWidth();
		greyScale();
		bmp = RgbImageAndroid.toBitmap(eRgbImage);
		return bmp;
	}
	
	public void preprocessing(){
		
	}
	
	public void transform(){
		
	}
	
	public void segmentation(){
		
	}
	
	public void filtering(){
		
	}
	
	public void featureExtraction(){
		
	}
	
	public void featureAnalysis(){
		
	}
	

	public void greyScale() {
		int[] rgbData = eRgbImage.getData();
		for(int y = 0; y < height; y++){
			int outputOffset = y*width;
			for(int x = 0;x < width; x++){
				int index = outputOffset + x;
				int R = ((rgbData[index]>>16) & 0xff);
				int G = ((rgbData[index]>>8) & 0xff);
				int B = ((rgbData[index]) & 0xff);
				int grey = (R + G + B)/3;
				rgbData[index] = 0xff000000 | (grey << 16) | (grey << 8) | grey;
			}
		}
	}
	
	public void sobel(){
		float[] template={-1,0,1,-2,0,2,-1,0,1};
		int templateSize=3;
		int[] rgbData = eRgbImage.getData();
		int[] total = new int[width*height];
		int sumY=0;
		int sumX=0;
		int max=0;
		for( int n = 0; n<1; n++){
			for(int x=(templateSize-1)/2; x<width-(templateSize+1)/2;x++){
				for(int y=(templateSize-1)/2; y<height-(templateSize+1)/2;y++){
					sumY=0;
					for(int x1=0;x1<templateSize;x1++){
						for(int y1=0;y1<templateSize;y1++){
							int x2 = (x-(templateSize-1)/2+x1);
							int y2 = (y-(templateSize-1)/2+y1);
							float value = (rgbData[y2*width+x2] & 0xff)*(template[y1*templateSize+x1]);
							sumY += value;
						}
					}
					sumX = 0;
					for(int x1=0;x1<templateSize;x1++){
						for(int y1=0;y1<templateSize;y1++){
							int x2 = (x-(templateSize-1)/2+x1);
							int y2 = (y-(templateSize-1)/2+y1);
							float value = (rgbData[y2*width+x2] & 0xff)*(template[x1*templateSize+y1]);
							sumX += value;
						}
					}
					total[y*width+x] = (int)Math.sqrt(sumX*sumX+sumY*sumY);
					if(max < total[y*width+x])
						max = total[y*width+x];
				}
			}
			float ratio=(float)max/255;
			for(int x=0; x<width;x++){
				for(int y=0; y<height;y++){
					sumX = (int)(total[y*width+x]/ratio);
					total[y*width+x] = 0xff000000 | ((int)sumX << 16 | (int)sumX << 8 |(int)sumX);
				}
			}
		}
		System.arraycopy(total,	0, rgbData,	0, width*height);
	}
	
	//Used into CameraMode for obtained image
	public static void setJpegData(Bitmap temp) {
		Bitmap bitmap = temp.copy(Bitmap.Config.ARGB_8888, true);
		eRgbImage = RgbImageAndroid.toRgbImage(bitmap);
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		bitmap.getPixels(eRgbImage.getData(), 0, width, 0, 0, width, height);
	}
	
	//Used into CameraMode for obtained image
	public static void setJpegData(byte[] jpegData) {
		Bitmap bitmap = BitmapFactory.decodeByteArray(jpegData, 0,
				jpegData.length, null);
		eRgbImage = RgbImageAndroid.toRgbImage(bitmap);
		width = bitmap.getWidth();
		height = bitmap.getHeight();
		bitmap.getPixels(eRgbImage.getData(), 0, width, 0, 0, width, height);
	}
}
	
	


